package cn.cleanarch.infra.idp.pms.constants;

import java.io.InputStream;

/**
 * 文件类型常量定义
 */
public final class FileTypeConstants {

    private FileTypeConstants() {
        // 防止实例化
    }

    // 设计工具文件类型
    public static final String AXURE_RP = "AXURE_RP";
    public static final String FIGMA = "FIGMA";
    public static final String SKETCH = "SKETCH";
    public static final String ADOBE_XD = "ADOBE_XD";
    public static final String PHOTOSHOP = "PHOTOSHOP";
    public static final String ILLUSTRATOR = "ILLUSTRATOR";
    public static final String INVISION = "INVISION";
    public static final String PRINCIPLE = "PRINCIPLE";
    public static final String FRAMER = "FRAMER";
    
    // 文档类型
    public static final String HTML_ZIP = "HTML_ZIP";
    public static final String HTML = "HTML";
    public static final String PDF = "PDF";
    public static final String DOC = "DOC";
    public static final String DOCX = "DOCX";
    public static final String XLS = "XLS";
    public static final String XLSX = "XLSX";
    public static final String PPT = "PPT";
    public static final String PPTX = "PPTX";
    
    // 图像类型
    public static final String IMAGE = "IMAGE";
    public static final String JPEG = "JPEG";
    public static final String PNG = "PNG";
    public static final String GIF = "GIF";
    public static final String SVG = "SVG";
    public static final String WEBP = "WEBP";
    public static final String BMP = "BMP";
    public static final String TIFF = "TIFF";
    public static final String ICO = "ICO";
    
    // 压缩包类型
    public static final String ZIP = "ZIP";
    public static final String RAR = "RAR";
    public static final String SEVEN_Z = "7Z";
    public static final String TAR = "TAR";
    public static final String GZ = "GZ";
    
    // 其他类型
    public static final String OTHER = "OTHER";
    public static final String UNKNOWN = "UNKNOWN";

    // 文件类型选项显示名称
    public static final String AXURE_RP_NAME = "Axure RP";
    public static final String FIGMA_NAME = "Figma";
    public static final String SKETCH_NAME = "Sketch";
    public static final String ADOBE_XD_NAME = "Adobe XD";
    public static final String PHOTOSHOP_NAME = "Adobe Photoshop";
    public static final String ILLUSTRATOR_NAME = "Adobe Illustrator";
    public static final String INVISION_NAME = "Invision";
    public static final String PRINCIPLE_NAME = "Principle";
    public static final String FRAMER_NAME = "Framer";
    public static final String HTML_ZIP_NAME = "HTML压缩包";
    public static final String HTML_NAME = "HTML文件";
    public static final String PDF_NAME = "PDF文档";
    public static final String DOC_NAME = "Word文档";
    public static final String DOCX_NAME = "Word文档";
    public static final String XLS_NAME = "Excel表格";
    public static final String XLSX_NAME = "Excel表格";
    public static final String PPT_NAME = "PowerPoint演示";
    public static final String PPTX_NAME = "PowerPoint演示";
    public static final String IMAGE_NAME = "图片";
    public static final String ZIP_NAME = "压缩包";
    public static final String OTHER_NAME = "其他";

    // 文件扩展名映射
    public static final String[] AXURE_EXTENSIONS = {".axure", ".rp"};
    public static final String[] FIGMA_EXTENSIONS = {".fig"};
    public static final String[] SKETCH_EXTENSIONS = {".sketch"};
    public static final String[] ADOBE_XD_EXTENSIONS = {".xd"};
    public static final String[] PHOTOSHOP_EXTENSIONS = {".psd"};
    public static final String[] ILLUSTRATOR_EXTENSIONS = {".ai"};
    public static final String[] INVISION_EXTENSIONS = {".invision"};
    public static final String[] PRINCIPLE_EXTENSIONS = {".principle"};
    public static final String[] FRAMER_EXTENSIONS = {".framer"};
    public static final String[] HTML_EXTENSIONS = {".html", ".htm"};
    public static final String[] PDF_EXTENSIONS = {".pdf"};
    public static final String[] DOC_EXTENSIONS = {".doc"};
    public static final String[] DOCX_EXTENSIONS = {".docx"};
    public static final String[] XLS_EXTENSIONS = {".xls"};
    public static final String[] XLSX_EXTENSIONS = {".xlsx"};
    public static final String[] PPT_EXTENSIONS = {".ppt"};
    public static final String[] PPTX_EXTENSIONS = {".pptx"};
    public static final String[] JPEG_EXTENSIONS = {".jpg", ".jpeg"};
    public static final String[] PNG_EXTENSIONS = {".png"};
    public static final String[] GIF_EXTENSIONS = {".gif"};
    public static final String[] SVG_EXTENSIONS = {".svg"};
    public static final String[] WEBP_EXTENSIONS = {".webp"};
    public static final String[] BMP_EXTENSIONS = {".bmp"};
    public static final String[] TIFF_EXTENSIONS = {".tiff", ".tif"};
    public static final String[] ICO_EXTENSIONS = {".ico"};
    public static final String[] ZIP_EXTENSIONS = {".zip"};
    public static final String[] RAR_EXTENSIONS = {".rar"};
    public static final String[] SEVEN_Z_EXTENSIONS = {".7z"};
    public static final String[] TAR_EXTENSIONS = {".tar"};
    public static final String[] GZ_EXTENSIONS = {".gz", ".gzip"};

    /**
     * 根据文件扩展名判断文件类型
     */
    public static String getFileTypeByExtension(String fileName) {
        if (fileName == null) return OTHER;
        
        String lowerName = fileName.toLowerCase();

        // HTML类型
        if (endsWithAny(lowerName, HTML_EXTENSIONS)) {
            return HTML;
        }
        
        // 设计工具文件
        if (endsWithAny(lowerName, AXURE_EXTENSIONS)) return AXURE_RP;
        if (endsWithAny(lowerName, FIGMA_EXTENSIONS)) return FIGMA;
        if (endsWithAny(lowerName, SKETCH_EXTENSIONS)) return SKETCH;
        if (endsWithAny(lowerName, ADOBE_XD_EXTENSIONS)) return ADOBE_XD;
        if (endsWithAny(lowerName, PHOTOSHOP_EXTENSIONS)) return PHOTOSHOP;
        if (endsWithAny(lowerName, ILLUSTRATOR_EXTENSIONS)) return ILLUSTRATOR;
        if (endsWithAny(lowerName, INVISION_EXTENSIONS)) return INVISION;
        if (endsWithAny(lowerName, PRINCIPLE_EXTENSIONS)) return PRINCIPLE;
        if (endsWithAny(lowerName, FRAMER_EXTENSIONS)) return FRAMER;
        
        // 文档文件
        if (endsWithAny(lowerName, PDF_EXTENSIONS)) return PDF;
        if (endsWithAny(lowerName, DOCX_EXTENSIONS)) return DOCX;
        if (endsWithAny(lowerName, DOC_EXTENSIONS)) return DOC;
        if (endsWithAny(lowerName, XLSX_EXTENSIONS)) return XLSX;
        if (endsWithAny(lowerName, XLS_EXTENSIONS)) return XLS;
        if (endsWithAny(lowerName, PPTX_EXTENSIONS)) return PPTX;
        if (endsWithAny(lowerName, PPT_EXTENSIONS)) return PPT;
        
        // 图像文件
        if (endsWithAny(lowerName, JPEG_EXTENSIONS)) return JPEG;
        if (endsWithAny(lowerName, PNG_EXTENSIONS)) return PNG;
        if (endsWithAny(lowerName, GIF_EXTENSIONS)) return GIF;
        if (endsWithAny(lowerName, SVG_EXTENSIONS)) return SVG;
        if (endsWithAny(lowerName, WEBP_EXTENSIONS)) return WEBP;
        if (endsWithAny(lowerName, BMP_EXTENSIONS)) return BMP;
        if (endsWithAny(lowerName, TIFF_EXTENSIONS)) return TIFF;
        if (endsWithAny(lowerName, ICO_EXTENSIONS)) return ICO;
        if (lowerName.matches("\\.(jpg|jpeg|png|gif|svg|bmp|webp|tiff|tif|ico)$")) {
            return IMAGE;
        }
        
        // 压缩包文件
        if (endsWithAny(lowerName, ZIP_EXTENSIONS)) return ZIP;
        if (endsWithAny(lowerName, RAR_EXTENSIONS)) return RAR;
        if (endsWithAny(lowerName, SEVEN_Z_EXTENSIONS)) return SEVEN_Z;
        if (endsWithAny(lowerName, TAR_EXTENSIONS)) return TAR;
        if (endsWithAny(lowerName, GZ_EXTENSIONS)) return GZ;
        
        return OTHER;
    }

    /**
     * 获取文件类型的显示名称
     */
    public static String getFileTypeName(String fileType) {
        switch (fileType) {
            case AXURE_RP: return AXURE_RP_NAME;
            case FIGMA: return FIGMA_NAME;
            case SKETCH: return SKETCH_NAME;
            case ADOBE_XD: return ADOBE_XD_NAME;
            case PHOTOSHOP: return PHOTOSHOP_NAME;
            case ILLUSTRATOR: return ILLUSTRATOR_NAME;
            case INVISION: return INVISION_NAME;
            case PRINCIPLE: return PRINCIPLE_NAME;
            case FRAMER: return FRAMER_NAME;
            case HTML_ZIP: return HTML_ZIP_NAME;
            case HTML: return HTML_NAME;
            case PDF: return PDF_NAME;
            case DOC: return DOC_NAME;
            case DOCX: return DOCX_NAME;
            case XLS: return XLS_NAME;
            case XLSX: return XLSX_NAME;
            case PPT: return PPT_NAME;
            case PPTX: return PPTX_NAME;
            case IMAGE: return IMAGE_NAME;
            case ZIP: return ZIP_NAME;
            case OTHER: return OTHER_NAME;
            default: return OTHER_NAME;
        }
    }

    /**
     * 检查字符串是否以指定的扩展名结尾
     */
    private static boolean endsWithAny(String str, String[] extensions) {
        for (String ext : extensions) {
            if (str.endsWith(ext)) {
                return true;
            }
        }
        return false;
    }
}
